<?php

class VideosController extends \BaseController {
    public $restful = true;
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        $user_id = 1;
        $nerds = Posts::whereIn('posts.user_id',Frelation::where('user_id','=',$user_id)->lists('friend_id'))
                    ->join('profile', 'posts.user_id', '=', 'profile.user_id')
                    ->select('posts.id','posts.title','posts.filename','posts.created_at','posts.play_count','profile.user_id','profile.fullname','profile.profile_picture')
                    ->orderBy('created_at', 'desc')
                    ->paginate(5);
        return Response::json(array('status' => 'success', 'state' => $nerds->toArray()));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function getCreate()
    {
        return View::make('upload.upload', array('num' => str_random(8)));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postUpload()
    {

        $poster_path = public_path().'/img/';
        $video_path = public_path().'/video/';
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'title'       => 'required',
            'poster'      => 'required',
            'video' => 'required'
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('video/create')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {
            // store
            if (Input::hasFile('poster')&&Input::hasFile('video'))
            {
                $file_poster = Input::file('poster');
                $poster_name = str_random(8) . '.' . $file_poster.'_'.time()->getClientOriginalExtension();
                $uploadSuccess1 = $file_poster->move($poster_path, $poster_name);

                $file_video =   Input::file('video');
                $video_name =   str_random(8) . '.' . $file_video.'_'.time()->getClientOriginalExtension();
                $uploadSuccess2   = $file_video->move($video_path, $video_name);
                $video = new Posts;
                $video->title = Input::get('title');
                $video->poster = URL::to('/').'/img/'.$poster_name;
                $video->filename = URL::to('/').'/video/'.$video_name;
                $video->user_id = 1;
                // TODO: 这里的user_id 对应登录的人，需要重新修改
                $video->created_at = time();
                $video->save();
                // redirect
                if( $uploadSuccess1 && $uploadSuccess2 ) {
                    return Response::json('success', 200); // or do a redirect with some message that file was uploaded
                } else {
                    return Response::json('error', 400);
                }
            }
            else {
                return Response::json('error', 400);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function getShow($id)
    {
        // get the nerd
        $nerd = Posts::find($id);
        $nerd->increment('play_count');
        $comments = Posts::find($id)->comments()->join('profile', 'comments.user_id', '=', 'profile.user_id')->get(['profile.user_id','profile.fullname','profile.profile_picture','comments.text']);
        $likes = Posts::find($id)->likes()->join('profile', 'likes.user_id', '=', 'profile.user_id')->get(['profile.user_id','profile.fullname','profile.profile_picture']);
        // show the view and pass the nerd to it
        return Response::json(array('status' => 'success', 'state' => $nerd->toArray(),'comments' => $comments->toArray(),'likes' => $likes->toArray()));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function getEdit($id)
    {
        return Response::json(array('status' => 'success', 'state' => "edit page"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function postUpdate($id)
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function postDestroy($id)
    {
        // delete
        $nerd = Posts::find($id);
        $nerd->delete();

        // redirect
        return Response::json('success', 200);
    }
}